package com.hxht.storage.server.dao.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hxht.storage.server.dao.NvrDeviceDao;
import com.hxht.storage.server.pojo.NvrDeviceInfo;

@Repository
public class NvrDeviceDaoImpl extends BaseDaoImpl<NvrDeviceInfo>
							  implements NvrDeviceDao{

	@PostConstruct
//	@PreDestroy
	public void init(){
		clazz = NvrDeviceInfo.class;
	}

	@Override
	public NvrDeviceInfo getNvrByIp(String ip) {

		NvrDeviceInfo nvr = null;
		
		TypedQuery<NvrDeviceInfo> query = entityManage.createQuery(
				"from NvrDeviceInfo where ip=:ip", clazz)
				.setParameter("ip", ip);
		
		List<NvrDeviceInfo> nvrList = query.getResultList();
		if (nvrList != null && nvrList.size()!= 0){
			nvr = nvrList.get(0);
		}
		
		return nvr;
	}

	@Override
	public boolean isNvrRegisted(String nvrIp) {
		// TODO Auto-generated method stub
		NvrDeviceInfo nvr = getNvrByIp(nvrIp);
		
		if (nvr== null){
			return false;
		}
		
		return true;
	}
	
	@Transactional(value="nvmpJpaEM", propagation=Propagation.REQUIRED)
	@Override
	public void insert(NvrDeviceInfo nvr) {
		entityManage.persist(nvr);
	}

	@Override
	public NvrDeviceInfo getNvrBySN(String sn) {

		NvrDeviceInfo nvr = null;
		
		TypedQuery<NvrDeviceInfo> query = entityManage.createQuery(
				"from NvrDeviceInfo where deviceId=:sn", clazz)
				.setParameter("sn", sn);
		
		List<NvrDeviceInfo> nvrList = query.getResultList();
		if (nvrList != null && nvrList.size()!= 0){
			nvr = nvrList.get(0);
		}
		
		return nvr;
	}
	
}
